package com.ehotting.edsta.circuitDesign.gen.arguments.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ScriptTypeEnum;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AssignmentArgumentsScript extends ArgumentsScriptAbstract {

    @Override
    public String invoke(EsRuleScript esScript,List<String> argumentsList, List<EsCircuitCodeDataTemplate> outputParamList, String functionName) {
        StringBuffer buf = new StringBuffer();
        buf.append(methodStart( functionName, argumentsList));
        buf.append(execute( esScript));
        buf.append(methodEnd());
        return buf.toString();
    }

    protected String execute(EsRuleScript esScript){
        StringBuffer buf = new StringBuffer();
        String[] lines = esScript.getContent().split(ScriptConstants.LINE);
        for(String line : lines){
            if(line.startsWith("ctx") || line.startsWith("all")) {
                buf.append(line).append(ScriptConstants.LINE);
            }else{
                String[] ss = line.split("=");
                //buf.append("def ").append(line).append(line.endsWith(";")?"":";").append(ScriptConstants.LINE);
                buf.append(ScriptConstants.TAB(1));
                buf.append("ctx."+ss[0]+"="+ss[1]+(line.endsWith(";")?"":";")).append(ScriptConstants.LINE);
            }

        }
        return buf.toString();
    }

    @Override
    public boolean isTrue(ScriptTypeEnum code){
        return ScriptTypeEnum.ASSIGNMENT == code;
    }

    @Override
    public String getExample() {
        StringBuffer buf = new StringBuffer();
        buf.append("输入格式:");
        buf.append("r1=200").append(ScriptConstants.LINE);
        buf.append("或者多行，回车隔开:");
        buf.append("r1=200").append(ScriptConstants.LINE);
        buf.append("r2=400").append(ScriptConstants.LINE);
        buf.append("或者范围值，分号隔开:");
        buf.append("r1Max=300;r1Min=100").append(ScriptConstants.LINE);
        buf.append("r2Max=400;r2Min=400").append(ScriptConstants.LINE);
        return buf.toString();
    }
}
